<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList">
      <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
      <el-table-column label="昵称" align="center" prop="nickname" width="200" />
      <el-table-column label="注册时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分销店铺名称" align="center" prop="shopname" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="warning"
            icon="el-icon-video-play"
            @click="handleShowUpGrade(scope.row)"
            v-hasPermi="['system:user:remove']"
            v-if="!scope.row.distributorId"
          >升级为分销商</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :closeOnClickModal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系电话" :disabled="this.form.id != null" />
        </el-form-item>
        <el-form-item label="分销商名称" prop="shopname">
          <el-input v-model="form.shopname" placeholder="请输入分销商名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMemberUser,upgradeUser,getUser } from "@/api/smy/member";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      groupTypeList:[
        {value: 'hotel', label: '酒店/住宿'},
        {value: 'restaurant', label: '饭店/餐饮'},
      ],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accesstoken: null,
        accountnonlocked: null,
        avatar: null,
        createtime: null,
        lastlogintime: null,
        loginip: null,
        mobile: null,
        nickname: null,
        password: null,
        realname: null,
        refreshtoken: null,
        username: null,
        roleId: null,
        email: null,
        maintype: null,
        merchantchecknote: null,
        merchantcheckstatus: null,
        qq: null,
        shopname: null,
        goodsnumber: null,
        merchantentertime: null,
        totalorderamount: null,
        totalordernumber: null,
        wechat: null,
        merchantenterpriseinfoId: null,
        merchantindividualinfoId: null,
        merchantpersonalinfoId: null,
        bankaccountholder: null,
        bankcardno: null,
        bankcity: null,
        bankmobile: null,
        bankname: null,
        bankprovince: null,
        banksubbranchname: null,
        signature: null,
        ordermessagenumber: null,
        systemmessagenumber: null,
        withdrawmessagenumber: null,
        openid: null,
        wechatsessionkey: null,
        firstlogintime: null,
        warehouseaddress: null,
        sex: null,
        balance: null,
        userno: null,
        merchantinvoicetfn: null,
        merchantinvoicetitle: null,
        merchantinvoicetitletype: null,
        selfsupport: null,
        shopbanner: null,
        relevancememberId: null,
        relevancemerchantId: null,
        certificate: null,
        firstlogin: null,
        fatherId: null,
        guaranteescale: null,
        distributorId: null,
        modifyrealnametime: null,
        anchorId: null,
        fatherexpires: null,
        modifynicknametime: null,
        liveidcardfront: null,
        liveidcardno: null,
        liveidcardreverse: null,
        livechecknote: null,
        livecheckstatus: null,
        liveid: null,
        liveimage: null,
        liveintroduce: null,
        livename: null,
        livebalance: null,
        livecreatetime: null,
        livestatus: null,
        livetype: null,
        liveSessionId: null,
        unionid: null,
        scandistributorid: null,
        merchantlat: null,
        merchantlng: null,
        lowestprice: null,
        closehour: null,
        closeminutes: null,
        openhour: null,
        openminutes: null,
        shopstatus: null,
        worktime: null,
        ratestar: null,
        attituderatestar: null,
        lookratestar: null,
        tasteratestar: null,
        completenum: null,
        rankno: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobile: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
        ],
        shopname: [
          { required: true, message: "分销商名称不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listMemberUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accesstoken: null,
        accountnonlocked: null,
        avatar: null,
        createtime: null,
        lastlogintime: null,
        loginip: null,
        mobile: null,
        nickname: null,
        password: null,
        realname: null,
        refreshtoken: null,
        username: null,
        roleId: null,
        email: null,
        maintype: null,
        merchantchecknote: null,
        merchantcheckstatus: 0,
        qq: null,
        shopname: null,
        goodsnumber: null,
        merchantentertime: null,
        totalorderamount: null,
        totalordernumber: null,
        wechat: null,
        merchantenterpriseinfoId: null,
        merchantindividualinfoId: null,
        merchantpersonalinfoId: null,
        bankaccountholder: null,
        bankcardno: null,
        bankcity: null,
        bankmobile: null,
        bankname: null,
        bankprovince: null,
        banksubbranchname: null,
        signature: null,
        ordermessagenumber: null,
        systemmessagenumber: null,
        withdrawmessagenumber: null,
        openid: null,
        wechatsessionkey: null,
        firstlogintime: null,
        warehouseaddress: null,
        sex: null,
        balance: null,
        userno: null,
        merchantinvoicetfn: null,
        merchantinvoicetitle: null,
        merchantinvoicetitletype: null,
        selfsupport: null,
        shopbanner: null,
        relevancememberId: null,
        relevancemerchantId: null,
        certificate: null,
        firstlogin: null,
        fatherId: null,
        guaranteescale: null,
        distributorId: null,
        modifyrealnametime: null,
        anchorId: null,
        fatherexpires: null,
        modifynicknametime: null,
        liveidcardfront: null,
        liveidcardno: null,
        liveidcardreverse: null,
        livechecknote: null,
        livecheckstatus: 0,
        liveid: null,
        liveimage: null,
        liveintroduce: null,
        livename: null,
        livebalance: null,
        livecreatetime: null,
        livestatus: 0,
        livetype: null,
        liveSessionId: null,
        unionid: null,
        scandistributorid: null,
        merchantlat: null,
        merchantlng: null,
        lowestprice: null,
        closehour: null,
        closeminutes: null,
        openhour: null,
        openminutes: null,
        shopstatus: "0",
        worktime: null,
        ratestar: null,
        attituderatestar: null,
        lookratestar: null,
        tasteratestar: null,
        completenum: null,
        rankno: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleShowUpGrade(row){
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let optDto = {
            optId:this.form.id,
            optName:this.form.shopname
          };
          if (this.form.id != null) {
            upgradeUser(optDto).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleUpgrade(row){
      const id = row.id;
      let optDto = {optId:id}
      this.$modal.confirm('是否确认将手机号码为"' + row.mobile + '"的用户升级为加盟商？').then(function() {
        return upgradeUser(optDto);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
  }
};
</script>
